package proxy;

/**
 * 
 */
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 委托接口调用处理程序
 * 
 * @author Andy
 * @see	java.lang.reflect.InvocationHandler
 * @since 2012.02.17 15:59 PM
 * @version 1.0
 */
public class AOPHandler implements InvocationHandler {

	private Object instance = null;
	
	private AOPHandler(Class<?> clazz){
		try {
			this.instance = clazz.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Object getProxyInstance(Class<?> clazz , Class<?> interfaces[]){
		return DynamicProxy.newInstance(clazz , interfaces , new AOPHandler(clazz));
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println(method.getName() + " invoke start.");
		Object returnValue = method.invoke(this.instance, args);
		System.out.println(method.getName() + " invoke end.");
		return returnValue;
	}
}

